import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
import Welcome from '../components/Welcome.vue'
import Users from '../components/user/Users.vue'
import Roles from '../components/right/Roles.vue'
import Rights from '../components/right/Rights.vue'
import Goods from '../components/good/Goods.vue'
import Params from '../components/good/Params.vue'
import Categories from '../components/good/Categories.vue'
import Add from '../components/good/Add.vue'
import Orders from '../components/order/Orders.vue'
import Reports from '../components/report/Reports.vue'



Vue.use(VueRouter)

// 挂载导航守卫
const router = new VueRouter({
    routes: [
        { path: '/', redirect: '/login' },
        { path: '/login', component: Login },
        {
            path: '/home',
            component: Home,
            redirect: '/welcome',
            children: [
                { path: '/welcome', component: Welcome },
                { path: '/users', component: Users },
                { path: '/roles', component: Roles },
                { path: '/rights', component: Rights },
                { path: '/goods', component: Goods },
                { path: '/params', component: Params },
                { path: '/Categories', component: Categories },
                { path: '/orders', component: Orders },
                { path: '/reports', component: Reports },
                { path: '/goods/add', component: Add }
            ]
        }
    ]
})

router.beforeEach((to, from, next) => {
    // to将要访问的路径
    // from代表从哪个路径来
    // next是一个函数,表示放行
    // next() 放行  next('/login') 强制跳转
    if (to.path === '/login') return next()
        // 获取token
    const tokenStr = window.sessionStorage.getItem('token')
    if (!tokenStr) return next('/login')
    next()
})
export default router